#!/bin/sh
#
# PROVIDE: opnsense-tayga
# REQUIRE: SERVERS
# KEYWORD: shutdown

. /etc/rc.subr

name=tayga

start_cmd=tayga_start
stop_cmd=tayga_stop
rcvar=tayga_enable

load_rc_config opnsense-tayga
pidfile=/var/run/${name}.pid
command=/usr/local/sbin/${name}
command_args="-p ${pidfile}"

[ -z "$tayga_enable" ] && tayga_enable="NO"

tayga_start()
{
        echo "starting tayga"
        ${command} ${command_args}
        sleep 1
        ifconfig nat64 inet ${tayga_v4destination}/32 ${tayga_v4address}
        ifconfig nat64 inet6 ${tayga_v6destination}/128
        ifconfig nat64 group tayga
        route -4 add ${tayga_v4pool} -interface nat64
        if [ "$tayga_v6routedisabled" != "YES" ]; then
            route -6 add ${tayga_v6prefix} -interface nat64
        fi
}

tayga_stop()
{
        if [ -n "$rc_pid" ]; then
            echo "stopping tayga"
            kill -2 ${rc_pid}
            ifconfig nat64 destroy
        else
            echo "${name} is not running."
        fi
}

run_rc_command $1
